import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:ball_lion/resources/colours.dart';
import 'package:ball_lion/utils/app_icons.dart';
import 'package:ball_lion/utils/user.dart';
import 'package:ball_lion/utils/widget_utils.dart';
import 'package:ball_lion/widgets/ball_appbar.dart';
import 'package:ball_lion/module/mine/widgets/list_cell_box.dart';
import 'package:ball_lion/widgets/base/wqs_dialog.dart';
import 'package:ball_lion/widgets/base/wqs_text.dart';

class MyAccountPage extends StatefulWidget {
  const MyAccountPage({super.key});

  @override
  State<MyAccountPage> createState() => _MyAccountPageState();
}

class _MyAccountPageState extends State<MyAccountPage> {
  tapCancelUser() async {
    final flag = await Get.dialog(const QsDialog.alert(
      '您确定注销账号吗？',
      content: '注销账号后个人数据将会被全部删除，请问您确认注销账号吗？',
    ));
    if (flag) {
      if (await User.cancelAccount()) {
        Get.close(2);
      }
    }
  }

  @override
  void initState() {
    super.initState();
    if (User.info == null) {
      User.fetchUserInfo().then((value) => update());
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Colours.scaffoldBg,
      appBar: const BallAppBar(title: "账号与安全"),
      body: Column(
        children: [
          ListCellBox(children: [
            cell("手机号", sub: User.securityPhoneNumber, arrow: false),
          ]).marginAll(8),
          ListCellBox(children: [
            cell("账号注销").tap(tapCancelUser),
          ]).marginSymmetric(horizontal: 8)
        ],
      ),
    );
  }

  Widget cell(String text, {String? sub, bool arrow = true}) => Container(
        padding: const EdgeInsets.symmetric(horizontal: 12),
        height: 46,
        child: Row(
          children: [
            QsText(
              text,
              fontSize: 14,
              color: Colours.N9,
            ),
            const Spacer(),
            if (sub != null)
              QsText(sub, color: Colours.N8).marginSymmetric(horizontal: 8),
            if (arrow) AppIcons.rightArrow()
          ],
        ),
      );
}
